package com.concerneyes.rentbar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.rentbar.dao.ShoppingCartBean;
import com.concerneyes.rentbar.listener.OnShoppingCartChangeListener;
import com.concerneyes.rentbar.util.ShoppingCartBiz;

import java.util.List;

import com.concerneyes.pay.DialogPay;
import util.ToastHelper;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class RentBarAdapter extends BaseAdapter {
    Context mContext;
    private List<ShoppingCartBean> mListGoods;
    private OnShoppingCartChangeListener mChangeListener;
    private boolean isSelectAll = false;
    public RentBarAdapter(Context mContext,List<ShoppingCartBean> mListGoods) {
        this.mContext = mContext;
        this.mListGoods = mListGoods;
        setSettleInfo();
    }

    public void setOnShoppingCartChangeListener(OnShoppingCartChangeListener changeListener) {
        this.mChangeListener = changeListener;
    }
    public void setList(List<ShoppingCartBean> mListGoods) {
        this.mListGoods = mListGoods;
        setSettleInfo();
    }

    public View.OnClickListener getAdapterListener() {
        return listener;
    }
    @Override
    public ShoppingCartBean getItem(int position) {
        return mListGoods.get(position);
    }

    @Override
    public int getCount() {
        return mListGoods.size();          //这里的目的是可以让图片循环浏览
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rent_bar_view, null);
            holder.tvItemtitle = (TextView) convertView.findViewById(R.id.tvItemtitle);
            holder.tvGoodsParam = (TextView) convertView.findViewById(R.id.tvGoodsParam);
            holder.tvGoodsParam1 = (TextView) convertView.findViewById(R.id.tvGoodsParam1);
            holder.tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            holder.ivCheckGood = (ImageView) convertView.findViewById(R.id.ivCheckGood);
            holder.ivGoods = (ImageView) convertView.findViewById(R.id.ivGoods);
            holder.ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            holder.ivReduce = (ImageView) convertView.findViewById(R.id.iv_reduce);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }
        holder.tvItemtitle.setText(mListGoods.get(position).getGoods().getGoodsName());
        holder.tvGoodsParam.setText(mListGoods.get(position).getGoods().getStatus());
        holder.tvPrice.setText("¥"+String.valueOf(mListGoods.get(position).getGoods().getPrice()));
        holder.tvNum.setText(String.valueOf(mListGoods.get(position).getGoods().getNumber()));
        ShoppingCartBiz.checkItem(mListGoods.get(position).isGoodsSelected(), holder.ivCheckGood);
        if (mListGoods.get(position).isGoodsSelected()){
            holder.ivCheckGood.setImageResource(R.drawable.ic_checked);
        }else {
            holder.ivCheckGood.setImageResource(R.drawable.ic_uncheck);
        }

        holder.ivAdd.setOnClickListener(listener);
        holder.ivReduce.setOnClickListener(listener);
        holder.ivCheckGood.setOnClickListener(listener);

        holder.ivAdd.setTag(mListGoods.get(position));
        holder.ivReduce.setTag(mListGoods.get(position));
        holder.ivCheckGood.setTag(position);
        return convertView;
    }

    View.OnClickListener listener = new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_choose_all:
                    isSelectAll = ShoppingCartBiz.selectAll(mListGoods, isSelectAll, (ImageView) v);
                    setSettleInfo();
                    notifyDataSetChanged();
                    break;
                case R.id.tv_balance:
                    if (ShoppingCartBiz.hasSelectedGoods(mListGoods)) {
                        new DialogPay(mContext,"1231651316",null);
                    } else {
                        ToastHelper.getInstance()._toast("亲，先选择商品！");
                    }
                    break;
                case R.id.ivCheckGood:
                    int groupPosition = Integer.parseInt(String.valueOf(v.getTag()));
                    isSelectAll = ShoppingCartBiz.selectGroup(mListGoods, groupPosition);
                    selectAll();
                    setSettleInfo();
                    notifyDataSetChanged();
                    break;
                case R.id.iv_add:
                    ShoppingCartBiz.addOrReduceGoodsNum(true, (ShoppingCartBean) v.getTag(), ((TextView) (((View) (v.getParent())).findViewById(R.id.tv_num))));
                    setSettleInfo();
                    notifyDataSetChanged();
                    break;
                case R.id.iv_reduce:
                    ShoppingCartBean bean = (ShoppingCartBean) v.getTag();
                    String currentNum = bean.getGoods().getNumber().trim();
                    int i = Integer.parseInt(currentNum);
                    if (i > 1) {
                        ShoppingCartBiz.addOrReduceGoodsNum(false, (ShoppingCartBean) v.getTag(), ((TextView) (((View) (v.getParent())).findViewById(R.id.tv_num))));
                        setSettleInfo();
                        notifyDataSetChanged();
                    }
                    break;
                case R.id.llGoodInfo:
                    ToastHelper.getInstance()._toast("商品详情，暂未实现");
                    break;
            }
        }
    };
    private void selectAll() {
        if (mChangeListener != null) {
            mChangeListener.onSelectItem(isSelectAll);
        }
    }

    private void setSettleInfo() {
        String[] infos = ShoppingCartBiz.getShoppingCount(mListGoods);
        //删除或者选择商品之后，需要通知结算按钮，更新自己的数据；
        if (mChangeListener != null && infos != null) {
            mChangeListener.onDataChange(infos[0], infos[1]);
        }
    }

    class viewHolder {
        TextView tvItemtitle;
        TextView tvGoodsParam;
        TextView tvGoodsParam1;
        TextView tvNum;
        TextView tvPrice;
        ImageView ivCheckGood;
        ImageView ivGoods;
        ImageView ivAdd;
        ImageView ivReduce;

    }
}
